package com.tarena.security.admin.service.impl;


import com.tarena.security.admin.entity.db.UserRole;
import com.tarena.security.admin.exception.SecurityAdminBusinessException;
import com.tarena.security.admin.param.UpdateUserRoleParam;
import com.tarena.security.admin.param.UserRoleParam;
import com.tarena.security.admin.persists.repository.UserRoleRepository;
import com.tarena.security.admin.service.IUserRoleService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.tarena.security.admin.errorenum.ResultEnum.SYSTEM_ERROR;
import static com.tarena.security.admin.errorenum.ResultEnum.SYS_USER_NON_EXISTENT;

/**
 * <p>
 * user_role 服务实现类
 * </p>
 *
 * @author Ly
 * @since 2023-04-10
 */
@Service
@Slf4j
public class UserRoleServiceImpl implements IUserRoleService {
    @Autowired
    UserRoleRepository userRoleRepository;

    @Override
    public int insertOne(UserRoleParam userRoleParam) {

        UserRole userRole=new UserRole();
        BeanUtils.copyProperties(userRoleParam,userRole);
        userRole.setGmtCreate(System.currentTimeMillis());
        log.info("新增用户与角色关系数据:{}",userRole);
        int num=userRoleRepository.insertOne(userRole);
        return num;
    }

    @Override
    public UserRole selectOne(Integer userId) {
        UserRole userRole=userRoleRepository.selectOne(userId);

        return userRole;
    }

    @Override
    public int deleteOneByUserId(Integer userId) {
        int num=userRoleRepository.deteleOneByUserId(userId);
        return num;
    }

    @Override
    public int updateByuserRole(UpdateUserRoleParam updateUserRoleParam) {

        userRoleRepository.selectById(updateUserRoleParam.getId());
        UserRole userRole=new UserRole();
        BeanUtils.copyProperties(updateUserRoleParam,userRole);
        userRole.setGmtCreate(System.currentTimeMillis());
        log.info("修改用户和角色关系数据:{}",userRole);
        int num=userRoleRepository.updateByuserRole(userRole);
        return num;
    }
}
